package com.yan.test.work.testpaging3

import android.content.Context
import com.yan.test.work.testpaging3.net.callback.IOnLoadDataListener
import com.yan.test.work.testpaging3.net.callback.IOnLoadListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

class MainCenterApi {

    val mainApi by lazy { MainApi() }
    
    suspend fun queryData(ctx: Context, keyWord:String, from:Int , size:Int = 20):List<DataBean>{
        return suspendCoroutine{continuation->

            mainApi.queryData(ctx , keyWord , from , size ,object:IOnLoadListener<DataBean>{
                override fun onSuccess(responseData: List<DataBean>) {
                    continuation.resume(responseData)
                }
                override fun onFailure(errorMessage: String?) {
                    continuation.resumeWithException( Exception(errorMessage))
                }
                override fun onFinish() {}
            })
        }


    }
}